package com.anhry.www.safety.manage.oa.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.anhry.www.safety.manage.enterprise.bean.TbCorpInfo;
import com.anhry.www.safety.manage.oa.bean.TbOaNotice;
import com.anhry.www.safety.manage.oa.bean.TbOaNoticeUser;
import com.anhry.www.safety.manage.oa.service.NoticeService;
import com.anhry.www.safety.manage.system.bean.TbUser;
import com.anhry.www.safety.util.dao.UtilDao;

/**
 * @author chen
 * @date 2012-8-13
 */
@Service
public class NoticeServiceImpl implements NoticeService {

	private static Logger logger = Logger.getLogger(NoticeServiceImpl.class);

	@Resource
	private UtilDao utilDao;

	public UtilDao getUtilDao() {
		return utilDao;
	}

	public List<TbOaNotice> loadNoticeList(TbUser user, String beginTime, String endTime, String keyWord, final int startRow, final int size) throws Exception {
		if (null == keyWord) {
			keyWord = "";
		}
		StringBuffer sbf = new StringBuffer("from TbOaNotice notice left join fetch notice.tbUser  left join fetch notice.tbUser.tbUserUnit   left join fetch notice.tbUser.tbUserDept  where notice.noticeTitle like '%" + keyWord + "%' and notice.isDele=0");
		if (size == 0) {
			sbf = new StringBuffer("select count(*) from TbOaNotice notice where notice.noticeTitle like '%" + keyWord + "%' and notice.isDele=0");
		}
		if (null != beginTime && !"".equals(beginTime) && null != endTime && !"".equals(endTime)) {
			sbf.append(" and to_char(notice.noticeTime,'yyyy-mm-dd') between '" + beginTime + "' and '" + endTime + "'");
		}
		if (null != user) {
			sbf.append(" and notice.tbUser.id='" + user.getId() + "'");
		}
		sbf.append(" order by notice.noticeTime desc");
		final String sql = sbf.toString();
		Session s = utilDao.getHibernateSession();
		List list = new ArrayList();
		try {
			Query query = null;
			query = s.createQuery(sql);
			if (size != 0) {
				query.setFirstResult(startRow);
				query.setMaxResults(size);
			}
			list = query.list();
		} finally {
			if (s != null && s.isOpen()) {
				s.close();
			}
		}

		return list;
	}

	public List<TbOaNotice> loadNoticeList(String beginTime, String endTime, String keyWord, String sendUser, String hql, String isNots, String isEs, final int startRow, final int size) throws Exception {
		if (null == keyWord) {
			keyWord = "";
		}
		StringBuffer sbf = new StringBuffer("from TbOaNotice notice left join fetch notice.tbUser  left join fetch notice.tbUser.tbUserUnit   left join fetch notice.tbUser.tbUserDept  where notice.noticeTitle like '%" + keyWord + "%' and notice.isDele=0");
		if (size == 0) {
			sbf = new StringBuffer("select count(*) from TbOaNotice notice where notice.noticeTitle like '%" + keyWord + "%' and notice.isDele=0");
		}
		if (null != beginTime && !"".equals(beginTime) && null != endTime && !"".equals(endTime)) {
			sbf.append(" and to_char(notice.noticeTime,'yyyy-mm-dd') between '" + beginTime + "' and '" + endTime + "'");
		}
		if (null != sendUser && !"".equals(sendUser)) {
			sbf.append("  and notice.tbUser.userRealName like '%" + sendUser + "%'");
		}
		if (null != isNots && !"".equals(isNots)) {
			sbf.append(" and notice.isNot='" + isNots + "'");
		}
		if (null != isEs && !"".equals(isEs)) {
			sbf.append(" and notice.isExigence='" + isEs + "'");
		}
		if (null != hql && !"".equals(hql)) {
			sbf.append(hql);
		}
		sbf.append(" order by notice.noticeTime desc");
		final String sql = sbf.toString();
		Session s = utilDao.getHibernateSession();
		List list = new ArrayList();
		try {
			Query query = null;
			query = s.createQuery(sql);
			if (size != 0) {
				query.setFirstResult(startRow);
				query.setMaxResults(size);
			}
			list = query.list();
		} finally {
			if (s != null && s.isOpen()) {
				s.close();
			}
		}

		return list;
	}

	public List<TbOaNoticeUser> loadInNoticeList(TbCorpInfo corp, String beginTime, String endTime, String keyWord, String isRead, final int startRow, final int size) throws Exception {
		if (null == keyWord) {
			keyWord = "";
		}
		StringBuffer sbf = new StringBuffer("from TbOaNoticeUser notice left join fetch notice.tbUser left join fetch notice.tbOaNotice    left join fetch notice.tbOaNotice.tbUser.tbUserUnit   left join fetch notice.tbOaNotice.tbUser.tbUserDept    where notice.tbOaNotice.noticeTitle like '%" + keyWord + "%' and notice.isDele=0 and notice.tbCorpInfo.id='" + corp.getId() + "' and notice.userType=1");
		if (size == 0) {
			sbf = new StringBuffer("select count(*) from TbOaNoticeUser notice where notice.tbOaNotice.noticeTitle like '%" + keyWord + "%' and notice.isDele=0 and notice.tbCorpInfo.id='" + corp.getId() + "' and notice.userType=1");
		}
		if (null != beginTime && !"".equals(beginTime) && null != endTime && !"".equals(endTime)) {
			sbf.append(" and to_char(notice.tbOaNotice.noticeTime,'yyyy-mm-dd') between '" + beginTime + "' and '" + endTime + "'");
		}
		if (null != isRead && !"".equals(isRead) && !"3".equals(isRead)) {// 3为全部
			sbf.append(" and  notice.isRead='" + isRead + "'");
		}

		sbf.append(" order by notice.tbOaNotice.noticeTime desc,notice.isRead");
		final String sql = sbf.toString();
		Session s = utilDao.getHibernateSession();
		List list = new ArrayList();
		try {
			Query query = null;
			query = s.createQuery(sql);
			if (size != 0) {
				query.setFirstResult(startRow);
				query.setMaxResults(size);
			}
			list = query.list();
		} finally {
			if (s != null && s.isOpen()) {
				s.close();
			}
		}
		return list;
	}

	public List<TbOaNoticeUser> loadInNoticeList(TbCorpInfo corp, String isRead, int size) throws Exception {
		StringBuffer sbf = new StringBuffer("from TbOaNoticeUser notice left join fetch notice.tbUser left join fetch notice.tbOaNotice    left join fetch notice.tbOaNotice.tbUser.tbUserUnit   left join fetch notice.tbOaNotice.tbUser.tbUserDept    where notice.isRead=" + isRead + " and notice.isDele=0 and notice.tbCorpInfo.id='" + corp.getId() + "' ");
		sbf.append(" order by notice.tbOaNotice.noticeTime desc");
		final String sql = sbf.toString();
		Session s = utilDao.getHibernateSession();
		List list = new ArrayList();
		try {
			Query query = null;
			query = s.createQuery(sql);
			query.setFirstResult(0);
			query.setMaxResults(size);
			list = query.list();
		} finally {
			if (s != null && s.isOpen()) {
				s.close();
			}
		}
		return list;
	}

	public int getNotReadNoticeSize(TbUser user) throws Exception {
		StringBuffer sbf = new StringBuffer();
		sbf = new StringBuffer("select count(*) from TbOaNoticeUser notice where notice.isDele=0 and notice.tbUser.id='" + user.getId() + "' and notice.isRead=0");
		final String sql = sbf.toString();
		Session s = utilDao.getHibernateSession();
		List list = new ArrayList();
		try {
			Query query = null;
			query = s.createQuery(sql);
			list = query.list();
		} finally {
			if (s != null && s.isOpen()) {
				s.close();
			}
		}
		return Integer.parseInt(list.get(0).toString());
	}

	public void setIsRead(Long id) throws Exception {
		StringBuffer sbf = new StringBuffer("update  TbOaNoticeUser   notice  set notice.isRead=0   where notice.tbOaNotice.id='" + id + "'");
		// left join fetch notice.tbCorpInfo.tbUserUnit
		final String sql = sbf.toString();
		Session s = utilDao.getHibernateSession();
		List list = new ArrayList();
		try {
			Query query = null;
			query = s.createQuery(sql);
			query.executeUpdate();
		} finally {
			if (s != null && s.isOpen()) {
				s.close();
			}
		}

	}

}
